﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class ChangePass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // if (Session["Employee"]==null||Session["Admin"]==null)
          //  Response.Write("<script>window.parent.location=\"login.aspx\";</script>");
    }
    protected void cmdChangePassword_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(new Connection().ConnectionString);
        con.Open();
        /* DataTable dt = new admin().LoadUser();
         foreach (DataRow row in dt.Rows)
         {
             if (txt_matkhau_cu.Text.Trim() != row[2].ToString().Trim())
             {
                 Label2.Visible = true;
                 return;
             }
            
          
         }*/
        if (Session["Employee"] != null)
        {
            string User_hientai = Session["Employee"].ToString();
            //string User_ht = Session["Admin"].ToString();
            int i;
            string password_cu = txt_matkhau_cu.Text;

            string strSQL = "select * from Admin1 where Username=@username and Password=@password ";
            SqlParameter[] thamso ={
                       new SqlParameter("@username",User_hientai),
                      // new SqlParameter("@username",User_ht),
                       new SqlParameter("@password",password_cu)
                              };
            DataTable dt1 = Connection.Truyvan_TraVe_Table_co_Thamso(strSQL, thamso);
            admin KTUserPass = new admin();
            i = KTUserPass.Kiemtra(User_hientai, password_cu);
            //i = KTUserPass.Kiemtra(User_ht, password_cu);
            if (i == 0) // Tài khoản và mật khẩu không khớp
            {
                Label2.Visible = true;
                Label2.Text = "ten user va mat khau hok khop";
                return;
            }
            else 
            {
                string matkhau_moi = txt_mk_moi.Text;
                string strSQL2 = "Update Admin1 Set Password=@password where Username=@username";
                SqlParameter[] thamso2 ={
                       new SqlParameter("@username",User_hientai),
                      //new SqlParameter("@username",User_ht),
                       new SqlParameter("@password",matkhau_moi)
                              };
                DataTable dt2 = Connection.Truyvan_TraVe_Table_co_Thamso(strSQL2, thamso2);
                TABLE1.Visible = false;
                Label1.Text = "Thay đổi thành công!";
            }
        }
        //admin
        else if (Session["Admin"] != null)
        {
            string User_ht = Session["Admin"].ToString();
            int ii;
            string password_cu1 = txt_matkhau_cu.Text;

            string strSQL1 = "select * from Admin1 where Username=@username and Password=@password";
            SqlParameter[] thamso3 ={
                           new SqlParameter("@username",User_ht),
                           new SqlParameter("@password",password_cu1)
                                  };
            DataTable dt3= Connection.Truyvan_TraVe_Table_co_Thamso(strSQL1, thamso3);
            admin KTUserPass = new admin();
            ii = KTUserPass.Kiemtra(User_ht, password_cu1);

            if (ii == 0) // Tài khoản và mật khẩu không khớp
            {
                Label2.Visible = true;
                Label2.Text = "ten user va mat khau hok khop";
                return;
            }
            else
            {
                string matkhau_moi1 = txt_mk_moi.Text;
                string strSQL3 = "Update Admin1 Set Password=@password where Username=@username";
                SqlParameter[] thamso4 ={
                           new SqlParameter("@username",User_ht),
                           new SqlParameter("@password",matkhau_moi1)
                                  };
                DataTable dt4 = Connection.Truyvan_TraVe_Table_co_Thamso(strSQL3, thamso4);
                TABLE1.Visible = false;
                Label1.Text = "Thay đổi thành công!";

            }
        }
            /*  AccessDataSource ads = new AccessDataSource();
              //ads.DataFile = "~/App_Data/EShopDB.mdb";
              ads.UpdateCommand = "UPDATE [Admin] set [Pass]='" + txt_mk_moi.Text.ToLower() + "' where [Email]='" + Session["Name"].ToString() + "'";
              ads.ConflictDetection = System.Web.UI.ConflictOptions.OverwriteChanges;
              ads.Update();
              TABLE1.Visible = false;
              Label1.Visible = true;
              Session.Clear();
              HttpCookie ck = new HttpCookie("Name", "");
              ck.Expires = DateTime.Now.AddHours(-1);
              Response.SetCookie(ck);
              ck = new HttpCookie("Pass", "");
              ck.Expires = DateTime.Now.AddHours(-1);
              Response.SetCookie(ck);*/

            //Cập nhật mật khẩu mới

        
    }
    protected void cancle_click(object sender, EventArgs e)
    {
        Response.Write("<script>window.parent.location=\"login.aspx\";</script>");
    }
}
